import 'package:core_foundation_example/pages/user/page_user_home.dart';
import 'package:flutter/material.dart';
import 'package:core_foundation/net/address.dart';
import 'package:core_foundation/net/http_manager.dart';
import 'package:core_foundation/utils/utils_logger.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'map/Page_map_home.dart';

class PageHome extends StatefulWidget {
  static const rName = "page_home";
  @override
  _PageHomeState createState() => _PageHomeState();
}

class _PageHomeState extends State<PageHome> with HomeBLOC, AutomaticKeepAliveClientMixin {


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            actions: <Widget>[
              RaisedButton(onPressed: ()=> Fluttertoast.showToast(msg: "Heelo"))
            ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            selectedItemColor: Colors.blueGrey,
            unselectedItemColor: Colors.black12,
            items: [
              BottomNavigationBarItem(title: Text("首页"), icon: Icon(Icons.home)),
              BottomNavigationBarItem(title: Text("地图"), icon: Icon(Icons.map)),
            ],
            currentIndex: _curIndex,
            onTap: (i) {
              _pageController.animateToPage(i, duration: Duration(milliseconds: 100), curve: Curves.ease);
            },
          ),
          body: PageView(
            controller: _pageController,
            onPageChanged: (i) {
              Logger.info("current Page");
              _curIndexChanged(i);
            },
            children: <Widget>[
              PageUserHome(),
              PageMapHome()
            ],
          ),
        ),
        onWillPop: willPop);
  }

  Widget _bottomBar() {
    return BottomAppBar(
      shape: CircularNotchedRectangle(),
      child: Row(
        children: <Widget>[],
      ),
    );
  }

  _body() {
    return Container(
      child: Center(
        child: Column(
          children: <Widget>[
            Text("这里是主页"),
            FlatButton(onPressed: ()=>networkTest(context), child: Text("网络请求，请查看console"))
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

}

mixin HomeBLOC on State<PageHome> {
  DateTime time;
  int _curIndex = 0;
  PageController _pageController;

  ///退出逻辑处理，两秒内不退出应用
  Future<bool> willPop() async {
    if (time == null || DateTime.now().difference(time) > Duration(seconds: 2)) {
      time = DateTime.now();
      Fluttertoast.showToast(msg: "再按一次退出应用");
      return false;
    }
    Logger.info("time $time \ncurrent :${DateTime.now()} \n${DateTime.now().difference(time)}", target: "page_home");
    return true;
  }
  @override
  initState() {
    super.initState();
    _pageController = PageController(initialPage: _curIndex);
  }

  _curIndexChanged(int index) {
    setState(() {
      _curIndex = index;
    });
  }


  networkTest(BuildContext context) async {
    var res = await httpManager.get(Address.host, context: context);
    print(res);
  }
}
